package com.example.demo.controller;

import ch.qos.logback.core.encoder.EchoEncoder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.LogonParams;
import com.example.demo.model.QueryDataModel;
import com.example.demo.model.QueryParams;
import com.example.demo.utils.HttpUtil;
import com.example.demo.utils.JBlowfish;
import com.example.demo.utils.TestTokenInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.net.URLEncoder;
import java.util.Arrays;

/**
 * 说明：
 *
 * @author 杨建全
 * @Date 2018/8/28 13:37
 */
@Controller
public class QueryController {

    public static final String FAIL = "FAIL";
    private Logger logger = LoggerFactory.getLogger(QueryController.class);

    @Value("${TestBaseUrl}")
    private String testBaseUrl;

    @Value("${UatBaseUrl}")
    private String uatBaseUrl;

    @Value("${ProdBaseUrl}")
    private String prodBaseUrl;


    @RequestMapping("/query")
    @ResponseBody
    public Object query(String data) throws Exception{

        logger.info("data is: {}",data);

        if (StringUtils.isEmpty(data)){
            return "FAIL";
        }

        QueryDataModel queryDataModel = JSON.parseObject(data,QueryDataModel.class);
        if (null == queryDataModel){
            return "FAIL";
        }
        String queryWhat = queryDataModel.getQueryWhat();
        switch (queryWhat){
            case "udidApiBankDetail":{
                return queryUdidApiBankDetail(queryDataModel);
            }
            case "udidApiBankVar":{
                return queryUdidApiBankVar(queryDataModel);

            }
            case "openApiBankVar":{
                return queryOpenApiBankVar(queryDataModel);

            }
            case "openApiBankDetail":{
                return queryOpenApiBankDetail(queryDataModel);

            }
            case "taobao":{
                return queryTaoBaoDetail(queryDataModel);

            }


            default:
                return FAIL;
        }
    }

    /**
     * 查询淘宝数据
     * @param queryDataModel
     * @return
     * @throws Exception
     */
    private Object queryTaoBaoDetail(QueryDataModel queryDataModel) throws Exception {

        LogonParams logonParams = new LogonParams();

        logonParams.setCardNiuId(queryDataModel.getCardNiuId());
        logonParams.setHistory(queryDataModel.getHistory());
        logonParams.setHouseName(queryDataModel.getHouseName());
        logonParams.setIdCardNo(queryDataModel.getIdCardNo());
        logonParams.setPhone(queryDataModel.getPhone());
        logonParams.setUdid(queryDataModel.getUdid());
        logonParams.setLoginName(queryDataModel.getLoginName());

        String logon = JSON.toJSONString(logonParams);
        String token = queryDataModel.getToken();
        String pwd = getToken(token, "wyzq",queryDataModel.getIsProd());
        String iv = Math.random() + "";// 其它的随机方式也可以
        logon = JBlowfish.encrypt(logon, pwd, iv);
        String url = getBaseUrl(queryDataModel.getIsProd())+"/getTaoBaoRawData?logon=" + URLEncoder.encode(logon, "utf-8") + "&token=" + token + "&iv=" + iv;

        String ret = HttpUtil.get(url,null);
        if (ret==null){
            return FAIL;
        }
        JSONObject jsonObject = JSON.parseObject(ret);
        if ("SUCCESS".equals(jsonObject.getString("ret"))) {
            return JBlowfish.decrypt(jsonObject.getString("data"), pwd, iv);
        } else {
            return (jsonObject.getString("errorCode") + ":" + jsonObject.getString("errorMsg"));
        }

    }

    /**
     * 开放接口查网银流水
     * @param queryDataModel
     * @return
     * @throws Exception
     */
    private Object queryOpenApiBankDetail(QueryDataModel queryDataModel) throws Exception{
        LogonParams logonParams = new LogonParams();

        logonParams.setCardNiuId(queryDataModel.getCardNiuId());
        logonParams.setHistory(queryDataModel.getHistory());
        logonParams.setHouseName(queryDataModel.getHouseName());
        logonParams.setIdCardNo(queryDataModel.getIdCardNo());
        logonParams.setPhone(queryDataModel.getPhone());
        logonParams.setUdid(queryDataModel.getUdid());
        logonParams.setLoginName(queryDataModel.getLoginName());

        String logon = JSON.toJSONString(logonParams);
        String token = queryDataModel.getToken();
        String pwd = getToken(token, "wyzq",queryDataModel.getIsProd());
        String iv = Math.random() + "";// 其它的随机方式也可以
        logon = JBlowfish.encrypt(logon, pwd, iv);
        String url = getBaseUrl(queryDataModel.getIsProd())+"/eBankDataDetails?logon=" + URLEncoder.encode(logon, "utf-8") + "&token=" + token + "&iv=" + iv;

        String ret = HttpUtil.get(url,null);
        if (ret==null){
            return FAIL;
        }
        JSONObject jsonObject = JSON.parseObject(ret);
        if ("SUCCESS".equals(jsonObject.getString("ret"))) {
            return JBlowfish.decrypt(jsonObject.getString("data"), pwd, iv);
        } else {
            return (jsonObject.getString("errorCode") + ":" + jsonObject.getString("errorMsg"));
        }
    }

    /**
     * udid查网银变量
     * @param queryDataModel
     * @return
     */
    private Object queryUdidApiBankVar(QueryDataModel queryDataModel) throws Exception{

        QueryParams queryParams = new QueryParams();

        queryParams.setUdidList(Arrays.asList(queryDataModel.getUdid()));
        queryParams.setHouseName(queryDataModel.getHouseName());
        queryParams.setIdCardNo(queryDataModel.getIdCardNo());
        queryParams.setKnUserId(queryDataModel.getCardNiuId());

        String queryString = JSON.toJSONString(queryParams);
        String token = queryDataModel.getToken();
        String pwd = getToken(token, "wyzq",queryDataModel.getIsProd());
        String iv = Math.random() + "";// 其它的随机方式也可以
        queryString = JBlowfish.encrypt(queryString, pwd, iv);

        String url = getBaseUrl(queryDataModel.getIsProd())+"/eBankDataVar2?queryString=" + URLEncoder.encode(queryString, "utf-8") + "&token=" + token + "&iv=" + iv;

        String ret = HttpUtil.get(url,null);
        if (ret==null){
            return FAIL;
        }
        JSONObject jsonObject = JSON.parseObject(ret);
        if ("SUCCESS".equals(jsonObject.getString("ret"))) {
            return JBlowfish.decrypt(jsonObject.getString("data"), pwd, iv);
        } else {
            return (jsonObject.getString("errorCode") + ":" + jsonObject.getString("errorMsg"));
        }
    }

    /**
     * 获取地址
     * @param isProd
     * @return
     */
    private String getBaseUrl(String isProd) {
        if ("1".equals(isProd)){
            return prodBaseUrl;
        }else if ("2".equals(isProd)){
            return uatBaseUrl;
        }
        return testBaseUrl;
    }

    /**
     * 开放变量接口查询
     * @param queryDataModel
     * @return
     */
    private Object queryOpenApiBankVar(QueryDataModel queryDataModel) throws Exception{

        LogonParams logonParams = new LogonParams();

        logonParams.setCardNiuId(queryDataModel.getCardNiuId());
        logonParams.setHistory(queryDataModel.getHistory());
        logonParams.setHouseName(queryDataModel.getHouseName());
        logonParams.setIdCardNo(queryDataModel.getIdCardNo());
        logonParams.setPhone(queryDataModel.getPhone());
        logonParams.setUdid(queryDataModel.getUdid());
        logonParams.setLoginName(queryDataModel.getLoginName());

        String logon = JSON.toJSONString(logonParams);
        String token = queryDataModel.getToken();
        String pwd = getToken(token, "wyzq",queryDataModel.getIsProd());
        String iv = Math.random() + "";// 其它的随机方式也可以
        logon = JBlowfish.encrypt(logon, pwd, iv);
        String url = getBaseUrl(queryDataModel.getIsProd())+"/eBankDataVar?logon=" + URLEncoder.encode(logon, "utf-8") + "&token=" + token + "&iv=" + iv;

        String ret = HttpUtil.get(url,null);
        if (ret==null){
            return FAIL;
        }
        JSONObject jsonObject = JSON.parseObject(ret);
        if ("SUCCESS".equals(jsonObject.getString("ret"))) {
            return JBlowfish.decrypt(jsonObject.getString("data"), pwd, iv);
        } else {
            return (jsonObject.getString("errorCode") + ":" + jsonObject.getString("errorMsg"));
        }
    }


    /**
     * udid取原始数据
     * @param queryDataModel
     * @return
     */
    private Object queryUdidApiBankDetail(QueryDataModel queryDataModel) throws Exception{

        QueryParams queryParams = new QueryParams();

        queryParams.setUdidList(Arrays.asList(queryDataModel.getUdid()));
        queryParams.setHouseName(queryDataModel.getHouseName());
        queryParams.setIdCardNo(queryDataModel.getIdCardNo());
        queryParams.setKnUserId(queryDataModel.getCardNiuId());

        String queryString = JSON.toJSONString(queryParams);
        String token = queryDataModel.getToken();
        String pwd = getToken(token, "wyzq",queryDataModel.getIsProd());
        String iv = Math.random() + "";// 其它的随机方式也可以
        queryString = JBlowfish.encrypt(queryString, pwd, iv);

        String url = getBaseUrl(queryDataModel.getIsProd())+"/eBankDataDetails2?queryString=" + URLEncoder.encode(queryString, "utf-8") + "&token=" + token + "&iv=" + iv;

        String ret = HttpUtil.get(url,null);
        if (ret==null){
            return FAIL;
        }
        JSONObject jsonObject = JSON.parseObject(ret);
        if ("SUCCESS".equals(jsonObject.getString("ret"))) {
            return JBlowfish.decrypt(jsonObject.getString("data"), pwd, iv);
        } else {
            return (jsonObject.getString("errorCode") + ":" + jsonObject.getString("errorMsg"));
        }
    }

    /**
     * 获取token
     * @param token
     * @param wyzq
     * @return
     */
    private String getToken(String token, String wyzq,String isProd) throws Exception{
        if (!"0".equals(isProd)){
            return TestTokenInfo.getProductTokenPwd(token, "wyzq");
        }
        return TestTokenInfo.getTestTokenPwd(token, "wyzq");
    }


}
